package com.example.project.key.service.employeeService;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.project.key.Dao.employeeDao.employeeDao;
import com.example.project.key.IBaseDao.IBaseDao_employee;
import com.example.project.key.connection.databaseconn;
import com.example.project.key.object.employee.employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class employeeService implements IBaseDao_employee {
    employeeDao empd = new employeeDao();
    private static PreparedStatement stmt = null;

    @Override
    public boolean add(Connection con,String dep, String worknum, String id, String tele, String gender, String mailbox, String age, String username,String pwd) {
       boolean f=empd.add(con,dep,worknum,id,tele,gender,mailbox,age,username,pwd);
        return f;
    }

    @Override
    public boolean delete(Connection con,String id, String password, String username) {
        return false;
    }

    @Override
    public boolean modify(Connection con,String dep, String worknum, String id, String tele, String gender, String mailbox, String age) {
        return false;
    }

    public boolean isExist(Connection conn,String id,String username){
        boolean f=true;
        if(conn!=null)
        {
            String sql="SELECT * FROM `employee` where `id` =? or username = ?";
            try {
                stmt=conn.prepareStatement(sql);
                stmt.setString(1,id);
                stmt.setString(2,username);
                ResultSet rs = stmt.executeQuery();
                if(rs!=null&&rs.next())
                {
                    return f;
                }else{
                    f=false;
                    return f;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                return f;
            }
        }else {
            f=true;
            return f;
        }
    }



    public boolean isRight(Connection conn, String username,String pwd){
        boolean f=false;
        if(conn!=null)
        {
            String sql="select * from employee where username = ? and password = ?";
            try {
                stmt=conn.prepareStatement(sql);
                stmt.setString(1,username);
                stmt.setString(2,pwd);
                ResultSet rs=stmt.executeQuery();
                if(rs!=null&&rs.next())
                {
                    f=true;
                }else {
                    f=false;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                f=false;
            }

        }else {
            f=false;
        }
        return f;
    }

    @Override
    public employee find(Connection con, String username, String pwd) {
        return empd.find(con,username,pwd) ;
    }

    public static boolean saveUserInfo(Context context, employee e)
    {
        SharedPreferences sp =context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username",e.getUsername());
        editor.putString("id",e.getId());
        editor.putString("department",e.getDepartment());
        editor.putString("worknum",e.getWorknum());
        editor.putString("telenum",e.getTelenum());
        editor.putString("sex",e.getSex());
        editor.putString("mailbox",e.getE_mail());
        editor.putString("age",e.getAge());
        editor.putString("password",e.getPwd());
        editor.commit();
        return true;
    }

    public static employee getInfo(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
        employee em = new employee();
        em.setUsername(sp.getString("username",""));
        em.setId(sp.getString("id",""));
        em.setDepartment(sp.getString("department",""));
        em.setWorknum(sp.getString("worknum",""));
        em.setTelenum(sp.getString("telenum",""));
        em.setSex(sp.getString("sex",""));
        em.setE_mail(sp.getString("mailbox",""));
        em.setAge(sp.getString("age",""));
        em.setPwd(sp.getString("password",""));
        return em;
    }

}
